package com.itheima.controller;

import com.itheima.Utils.QiniuUtil;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.PhysicalUpload;
import com.itheima.service.PhysicalUploadService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/physical")
public class PhysicalUploadController {

    @Reference
    private PhysicalUploadService physicalUploadService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            return physicalUploadService.pageQuery(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,String> map,Integer[] setmealIds) {
        try {
            if (setmealIds.length==1) {
                physicalUploadService.add(map,setmealIds);
                return new Result(true,"添加成功");

            }else {
                return new Result(false,"体检套餐必须选择一个,不能不选或多选");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("PDFFile") MultipartFile PDFFile){
        try {
            String originalFilename = PDFFile.getOriginalFilename();
            //获取最后一个.的位置
            int index = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(index - 1);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = "report" +  UUID.randomUUID().toString() + suffix;
            //图片上传成功
            QiniuUtil.uploadQiniu(PDFFile.getBytes(),fileName);

            return new Result(true,"上传报告成功",fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "上传报告失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            PhysicalUpload physicalUpload = physicalUploadService.findById(id);
            return new Result(true,"查找成功",physicalUpload);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查找失败");
        }
    }

    @RequestMapping("/uploadReport")
    public Result uploadReport(@RequestBody PhysicalUpload physicalUpload) {
        try {
            physicalUploadService.uploadReport(physicalUpload);
            return new Result(true,"上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"上传失败");
        }
    }

    @RequestMapping("/deletebyid")
    public Result deletebyid(Integer id) {
        try {
            physicalUploadService.deletebyid(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

}

